package team.knowers.qzhserver.user.userFollower.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.knowers.qzhserver.common.api.CommonResult;
import team.knowers.qzhserver.user.user.service.UserService;
import team.knowers.qzhserver.user.userFollower.service.UserFollowerService;

/**
 * @author zcb
 * @date 2024/3/5 20:26
 */
@Tag(name = "UserFollowerController", description = "用户关注信息")
@RequestMapping("/userFollower")
@Controller
public class UserFollowerController {
    @Autowired
    private UserFollowerService userFollowerService;
    @Autowired
    private UserService userService;
    @PostMapping(value = "/follow")
    @Operation(summary = "fid关注uid")
    @ResponseBody
    public CommonResult follow(@RequestParam("fid") String fid,@RequestParam("uid") String uid){
        //防止传回不合理的参数
        if(fid.equals(uid)){
            return  CommonResult.failed("不能关注自己");
        }
        if(userService.userStatus(uid)==-1 || userService.userStatus(fid)==-1)
            return CommonResult.failed("用户id不存在");
        Boolean status = userFollowerService.follow(fid,uid);
        if (status){
            return CommonResult.success(fid+"已成功关注"+uid);
        }else {
            return CommonResult.failed(fid+"已关注过"+uid+"，不能重复关注");
        }
    }
    @DeleteMapping(value = "/unfollow")
    @Operation(summary = "fid取消关注uid")
    @ResponseBody
    public CommonResult unfollow(@RequestParam("fid") String fid,@RequestParam("uid") String uid){
        //防止传回不合理的参数
        if(fid.equals(uid)){
            return  CommonResult.failed("不能关注自己");
        }
        if(userService.userStatus(uid)==-1 || userService.userStatus(fid)==-1)
            return CommonResult.failed("用户id不存在");
        Boolean status = userFollowerService.unfollow(fid,uid);
        if (status){
            return CommonResult.success(fid+"已成功取消关注"+uid);
        }else {
            return CommonResult.failed(fid+"没有关注过"+uid);
        }
    }
}
